home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / domacnost a kancelar / joomla / Joomla_1.5.4-Stable-Full_Package.exe / plugins / search / content.php < prev    next >
PHP Script  |  2008-07-06  |  8KB  |  256 lines

  1. <?php
  2. /**
  3.  * @version        $Id: content.php 10381 2008-06-01 03:35:53Z pasamio $
  4.  * @package        Joomla
  5.  * @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  6.  * @license        GNU/GPL, see LICENSE.php
  7.  * Joomla! is free software. This version may have been modified pursuant
  8.  * to the GNU General Public License, and as distributed it includes or
  9.  * is derivative of works licensed under the GNU General Public License or
  10.  * other free or open source software licenses.
  11.  * See COPYRIGHT.php for copyright notices and details.
  12.  */
  13.  
  14. // no direct access
  15. defined( '_JEXEC' ) or die( 'Restricted access' );
  16.  
  17. $mainframe->registerEvent( 'onSearch', 'plgSearchContent' );
  18. $mainframe->registerEvent( 'onSearchAreas', 'plgSearchContentAreas' );
  19.  
  20. JPlugin::loadLanguage( 'plg_search_content' );
  21.  
  22. /**
  23.  * @return array An array of search areas
  24.  */
  25. function &plgSearchContentAreas()
  26. {
  27.     static $areas = array(
  28.         'content' => 'Articles'
  29.     );
  30.     return $areas;
  31. }
  32.  
  33. /**
  34.  * Content Search method
  35.  * The sql must return the following fields that are used in a common display
  36.  * routine: href, title, section, created, text, browsernav
  37.  * @param string Target search string
  38.  * @param string mathcing option, exact|any|all
  39.  * @param string ordering option, newest|oldest|popular|alpha|category
  40.  * @param mixed An array if the search it to be restricted to areas, null if search all
  41.  */
  42. function plgSearchContent( $text, $phrase='', $ordering='', $areas=null )
  43. {
  44.     global $mainframe;
  45.  
  46.     $db        =& JFactory::getDBO();
  47.     $user    =& JFactory::getUser();
  48.  
  49.     require_once(JPATH_SITE.DS.'components'.DS.'com_content'.DS.'helpers'.DS.'route.php');
  50.  
  51.     if (is_array( $areas )) {
  52.         if (!array_intersect( $areas, array_keys( plgSearchContentAreas() ) )) {
  53.             return array();
  54.         }
  55.     }
  56.  
  57.     // load plugin params info
  58.      $plugin            =& JPluginHelper::getPlugin('search', 'content');
  59.      $pluginParams    = new JParameter( $plugin->params );
  60.  
  61.     $sContent         = $pluginParams->get( 'search_content',         1 );
  62.     $sUncategorised = $pluginParams->get( 'search_uncategorised',     1 );
  63.     $sArchived         = $pluginParams->get( 'search_archived',         1 );
  64.     $limit             = $pluginParams->def( 'search_limit',         50 );
  65.  
  66.     $nullDate         = $db->getNullDate();
  67.     $date =& JFactory::getDate();
  68.     $now = $date->toMySQL();
  69.  
  70.     $text = trim( $text );
  71.     if ($text == '') {
  72.         return array();
  73.     }
  74.  
  75.     $wheres = array();
  76.     switch ($phrase) {
  77.         case 'exact':
  78.             $text        = $db->Quote( '%'.$db->getEscaped( $text, true ).'%', false );
  79.             $wheres2     = array();
  80.             $wheres2[]     = 'LOWER(a.title) LIKE '.$text;
  81.             $wheres2[]     = 'LOWER(a.introtext) LIKE '.$text;
  82.             $wheres2[]     = 'LOWER(a.`fulltext`) LIKE '.$text;
  83.             $wheres2[]     = 'LOWER(a.metakey) LIKE '.$text;
  84.             $wheres2[]     = 'LOWER(a.metadesc) LIKE '.$text;
  85.             $where         = '(' . implode( ') OR (', $wheres2 ) . ')';
  86.             break;
  87.  
  88.         case 'all':
  89.         case 'any':
  90.         default:
  91.             $words = explode( ' ', $text );
  92.             $wheres = array();
  93.             foreach ($words as $word) {
  94.                 $word        = $db->Quote( '%'.$db->getEscaped( $word, true ).'%', false );
  95.                 $wheres2     = array();
  96.                 $wheres2[]     = 'LOWER(a.title) LIKE '.$word;
  97.                 $wheres2[]     = 'LOWER(a.introtext) LIKE '.$word;
  98.                 $wheres2[]     = 'LOWER(a.`fulltext`) LIKE '.$word;
  99.                 $wheres2[]     = 'LOWER(a.metakey) LIKE '.$word;
  100.                 $wheres2[]     = 'LOWER(a.metadesc) LIKE '.$word;
  101.                 $wheres[]     = implode( ' OR ', $wheres2 );
  102.             }
  103.             $where = '(' . implode( ($phrase == 'all' ? ') AND (' : ') OR ('), $wheres ) . ')';
  104.             break;
  105.     }
  106.  
  107.     $morder = '';
  108.     switch ($ordering) {
  109.         case 'oldest':
  110.             $order = 'a.created ASC';
  111.             break;
  112.  
  113.         case 'popular':
  114.             $order = 'a.hits DESC';
  115.             break;
  116.  
  117.         case 'alpha':
  118.             $order = 'a.title ASC';
  119.             break;
  120.  
  121.         case 'category':
  122.             $order = 'b.title ASC, a.title ASC';
  123.             $morder = 'a.title ASC';
  124.             break;
  125.  
  126.         case 'newest':
  127.             default:
  128.             $order = 'a.created DESC';
  129.             break;
  130.     }
  131.  
  132.     $rows = array();
  133.  
  134.     // search articles
  135.     if ( $sContent && $limit > 0 )
  136.     {
  137.         $query = 'SELECT a.title AS title,'
  138.         . ' a.created AS created,'
  139.         . ' CONCAT(a.introtext, a.`fulltext`) AS text,'
  140.         . ' CONCAT_WS( "/", u.title, b.title ) AS section,'
  141.         . ' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'
  142.         . ' CASE WHEN CHAR_LENGTH(b.alias) THEN CONCAT_WS(":", b.id, b.alias) ELSE b.id END as catslug,'
  143.         . ' u.id AS sectionid,'
  144.         . ' "2" AS browsernav'
  145.         . ' FROM #__content AS a'
  146.         . ' INNER JOIN #__categories AS b ON b.id=a.catid'
  147.         . ' INNER JOIN #__sections AS u ON u.id = a.sectionid'
  148.         . ' WHERE ( '.$where.' )'
  149.         . ' AND a.state = 1'
  150.         . ' AND u.published = 1'
  151.         . ' AND b.published = 1'
  152.         . ' AND a.access <= '.(int) $user->get( 'aid' )
  153.         . ' AND b.access <= '.(int) $user->get( 'aid' )
  154.         . ' AND u.access <= '.(int) $user->get( 'aid' )
  155.         . ' AND ( a.publish_up = '.$db->Quote($nullDate).' OR a.publish_up <= '.$db->Quote($now).' )'
  156.         . ' AND ( a.publish_down = '.$db->Quote($nullDate).' OR a.publish_down >= '.$db->Quote($now).' )'
  157.         . ' GROUP BY a.id'
  158.         . ' ORDER BY '. $order
  159.         ;
  160.         $db->setQuery( $query, 0, $limit );
  161.         $list = $db->loadObjectList();
  162.         $limit -= count($list);
  163.  
  164.         if(isset($list))
  165.         {
  166.             foreach($list as $key => $item)
  167.             {
  168.                 $list[$key]->href = ContentHelperRoute::getArticleRoute($item->slug, $item->catslug, $item->sectionid);
  169.             }
  170.         }
  171.         $rows[] = $list;
  172.     }
  173.  
  174.     // search uncategorised content
  175.     if ( $sUncategorised && $limit > 0 )
  176.     {
  177.         $query = 'SELECT id, a.title AS title, a.created AS created,'
  178.         . ' a.introtext AS text,'
  179.         . ' "2" as browsernav, "'. $db->Quote(JText::_('Uncategorised Content')) .'" AS section'
  180.         . ' FROM #__content AS a'
  181.         . ' WHERE ('.$where.')'
  182.         . ' AND a.state = 1'
  183.         . ' AND a.access <= '.(int) $user->get( 'aid' )
  184.         . ' AND a.sectionid = 0'
  185.         . ' AND a.catid = 0'
  186.         . ' AND ( a.publish_up = '.$db->Quote($nullDate).' OR a.publish_up <= '.$db->Quote($now).' )'
  187.         . ' AND ( a.publish_down = '.$db->Quote($nullDate).' OR a.publish_down >= '.$db->Quote($now).' )'
  188.         . ' ORDER BY '. ($morder ? $morder : $order)
  189.         ;
  190.         $db->setQuery( $query, 0, $limit );
  191.         $list2 = $db->loadObjectList();
  192.         $limit -= count($list2);
  193.  
  194.         if(isset($list2))
  195.         {
  196.             foreach($list2 as $key => $item)
  197.             {
  198.                 $list2[$key]->href = ContentHelperRoute::getArticleRoute($item->id);
  199.             }
  200.         }
  201.  
  202.         $rows[] = $list2;
  203.     }
  204.  
  205.     // search archived content
  206.     if ( $sArchived && $limit > 0 )
  207.     {
  208.         $searchArchived = JText::_( 'Archived' );
  209.  
  210.         $query = 'SELECT a.title AS title,'
  211.         . ' a.created AS created,'
  212.         . ' a.introtext AS text,'
  213.         . ' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'
  214.         . ' CASE WHEN CHAR_LENGTH(b.alias) THEN CONCAT_WS(":", b.id, b.alias) ELSE b.id END as catslug,'
  215.         . ' u.id AS sectionid,'
  216.         . ' "2" AS browsernav'
  217.         . ' FROM #__content AS a'
  218.         . ' INNER JOIN #__categories AS b ON b.id=a.catid AND b.access <= ' .$user->get( 'gid' )
  219.         . ' INNER JOIN #__sections AS u ON u.id = a.sectionid'
  220.         . ' WHERE ( '.$where.' )'
  221.         . ' AND a.state = -1'
  222.         . ' AND u.published = 1'
  223.         . ' AND b.published = 1'
  224.         . ' AND a.access <= '.(int) $user->get( 'aid' )
  225.         . ' AND b.access <= '.(int) $user->get( 'aid' )
  226.         . ' AND u.access <= '.(int) $user->get( 'aid' )
  227.         . ' AND ( a.publish_up = '.$db->Quote($nullDate).' OR a.publish_up <= '.$db->Quote($now).' )'
  228.         . ' AND ( a.publish_down = '.$db->Quote($nullDate).' OR a.publish_down >= '.$db->Quote($now).' )'
  229.         . ' ORDER BY '. $order
  230.         ;
  231.         $db->setQuery( $query, 0, $limit );
  232.         $list3 = $db->loadObjectList();
  233.  
  234.         if(isset($list3))
  235.         {
  236.             foreach($list3 as $key => $item)
  237.             {
  238.                 $list3[$key]->href = ContentHelperRoute::getArticleRoute($item->slug, $item->catslug, $item->sectionid);
  239.             }
  240.         }
  241.  
  242.         $rows[] = $list3;
  243.     }
  244.  
  245.     $results = array();
  246.     if(count($rows))
  247.     {
  248.         foreach($rows as $row)
  249.         {
  250.             $results = array_merge($results, (array) $row);
  251.         }
  252.     }
  253.  
  254.     return $results;
  255. }
  256.